package imagecutter.amra;

import java.awt.image.BufferedImage;
import java.util.Collection;

public class Main {
    private Configuration config;
    private String propertiesDefaultFilename = "application.properties";

    public Main(){
        init();
    }

    private  void init(){
        try {
            config = new Configuration(propertiesDefaultFilename);
        } catch (Exception ex) {
            exit("Cannot load configuration file: "+ex.getMessage());
        }
    }

    private void exit(String message){
        System.out.println(message);
        System.out.println("Exiting.");
        System.exit(0);
    }

    public void cut(){
        Collection<Rectangle> rectangleConfig =  config.loadCutConfiguration();
        BufferedImage originalImage = FileOperation.loadImageFromFile(config.getSourceImageFile());
        for(Rectangle rect: rectangleConfig){
            BufferedImage subImage = ImageOperation.subImage(originalImage, rect);
            FileOperation.saveImagetoFile(subImage, config.getNextSubImageFile());
        }
    }

    public static void main(String[] args) {
        new Main().cut();
    }
}
